package com.usian.skill.config;

import com.alibaba.fastjson.JSON;
import com.usian.skill.mapper.GoodsMapper;
import com.usian.skill.vo.GoodsVo;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;

@Component
public class AddRedis {

    @Resource
    private GoodsMapper goodsMapper;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @PostConstruct
    public void reload(){
        List<GoodsVo> goodsVos=goodsMapper.qclist();
        if (!CollectionUtils.isEmpty(goodsVos)){
            for (GoodsVo vo : goodsVos) {
                stringRedisTemplate.opsForValue().set(vo.getId().toString(), JSON.toJSONString(vo));

            }
        }
        stringRedisTemplate.opsForValue().set("goods",JSON.toJSONString(goodsVos));


    }


}
